package com.aliyun.oss.userController;

import com.aliyun.oss.UserService.AddressBookService;
import com.aliyun.oss.pojo.Entity.addressBook;
import com.aliyun.oss.pojo.Vo.ResultVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/addressBook")
public class AddressBookController {
    @Autowired
    AddressBookService addressBookService;

    //    查询所有地址
    @GetMapping("/list")
    public ResultVO list(@RequestAttribute("id") Integer id) {
        return ResultVO.success(addressBookService.list(new QueryWrapper<addressBook>().eq("user_id", id)));
    }

    //    新增地址
    @PostMapping("/add")
    public ResultVO add(@RequestAttribute("id") Integer id, @RequestBody addressBook addressBook) {
        addressBookService.addAddressBook(id, addressBook);
        return ResultVO.success();
    }

    //    根据id查询地址
    @GetMapping("/getById/{id}")
    public ResultVO getById(@PathVariable Integer id) {
        return ResultVO.success(addressBookService.getById(id));
    }

    //    根据id更新地址
    @PatchMapping("/update")
    public ResultVO update(@RequestBody addressBook addressBook) {
        return ResultVO.success(addressBookService.updateById(addressBook));
    }

    //    设置默认地址
    @PatchMapping("/setDefault/{id}")
    public ResultVO setDefault(@RequestAttribute("id") Integer id, @PathVariable("id") Integer isDefault) {
        addressBookService.setDefault(id, isDefault);
        return ResultVO.success();
    }

    //    根据id删除地址
    @DeleteMapping("/delete/{id}")
    public ResultVO delete(@PathVariable Integer id) {
        addressBookService.remove(new QueryWrapper<addressBook>().eq("id", id));
        return ResultVO.success();
    }
//    查询默认地址
    @GetMapping("/getDefault")
    public ResultVO getDefault(@RequestAttribute("id") Integer id) {
        return ResultVO.success(addressBookService.getOne(new QueryWrapper<addressBook>().eq("user_id", id).eq("is_default", 1)));
    }
}
